import loadjs from 'loadjs'

let isInited = false

const load = (src, id) => {
  return new Promise((resolve, reject) => {
    loadjs([src], id, () => {
      resolve()
    })
  })
}

export default async (src, id) => {
  return new Promise(resolve => {
    if (isInited) {
      // 等待变量存在
      const timer = setInterval(() => {
        if (window[id]) {
          resolve()
          clearInterval(timer)
        }
      }, 100)
    }
    isInited = true
    load(src, id)
      .then(() => {
        resolve()
      })
  })
}
